package FillMythMetadataVideoFileUtilities;
use strict;

sub get_video_length
{
	my $fullfilename = $_[0];
	my ($video_len) = grep(/^ID_LENGTH=/, (`mplayer -identify \"$fullfilename\" -nosound -vc dummy -vo null -nosub -noautosub`));
	#in DVD rips (VIDEO_TS), the mplayer will not return the length.
	if (($video_len) && ($video_len =~ /^[^=]+=(.*)$/ ))
	{
		$video_len = 0+$1;
		#myth like it in minutes
		$video_len = $video_len / 60;
	}
	else
	{
		$video_len = 0;
	}
	return $video_len;
}

sub create_video_thumb
{
	my $fullfilename = $_[0];
	my $video_length = $_[1];
	my $thumb_path = $_[2];
	my $video_thumb_offset = $_[3];
	my $snapshot_width = $_[4];
	
	my $thumb_offset;
	#it comes in minutes. I need in seconds.
	$video_length = $video_length * 60;
	
	if ($video_thumb_offset > 0)
	{
		if ($video_thumb_offset >= $video_length)
		{
			$thumb_offset = int($video_length * 0.5);
		}
		else
		{
			$thumb_offset = int($video_thumb_offset);
		}
	}
	else
	{
		$thumb_offset = int((0.3 + (rand() * 0.5)) * $video_length);
		print "Random point at second: $thumb_offset\n";
	}

	if ($thumb_offset <= 0)
	{
		print "Video snapshot point is ZERO! Can not create snapshot.\n";
	}
	else
	{
		my $cmd = "mplayer -zoom -ss $thumb_offset -nosound  -nosub -noautosub -vo jpeg:outdir=/tmp -frames 1 -vf scale=$snapshot_width:-3 \"$fullfilename\" > /dev/null";
		system($cmd);
		system ("mv /tmp/*1.jpg \"$thumb_path\"");
	}
}

1;
